/**
* @File:mfrc522_example.c
* @Version:V1.0
* @Author:rudius
* @Brief:
* @Logs:
* 	Date		Author		Notes
* 	2025-06-24	rudius		first version
*/

// <<< Use Configuration Wizard in Context Menu >>>

/* Private includes --------------------------------------------------------- */
#include "mfrc522.h"
#include "drv_gpio.h"
#ifdef __cplusplus
extern "C" {
#endif

/* Private config ----------------------------------------------------------- */
/* Private defines ---------------------------------------------------------- */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
/* Private function prototypes ---------------------------------------------- */

/*----------------------------------------------------------------------------*/
/* Private functions -------------------------------------------------------- */
/* mfrc522_example.c */
static void mfrc522_test_thread_entry(void *parameter)
{
    // 设备实例改为指针（全局内存）
    mfrc522_device *dev = RT_NULL;
    MFRC522_Uid uid;
    uint8_t retry_count = 0;
    const char *dev_name = "mfrc5220"; // 设备名称
    const char *i2c_bus = "i2c1";      // I2C总线名称
    uint8_t i2c_addr = 0x28;           // I2C设备地址
    rt_base_t rst_pin = GET_PIN(A, 8); // 复位引脚

    rt_kprintf("Starting MFRC522 Test...\n");
    
    // 创建设备对象（全局内存）
    dev = (mfrc522_device*)rt_malloc(sizeof(mfrc522_device));
    if (dev == RT_NULL) {
        rt_kprintf("Memory allocation failed!\n");
        return;
    }
    rt_memset(dev, 0, sizeof(mfrc522_device));

    // 初始化设备对象
    rt_err_t result = mfrc522_init(dev, i2c_bus, i2c_addr, rst_pin, dev_name);
    if (result != RT_EOK) {
        rt_kprintf("Device initialization failed! Error: %d\n", result);
        rt_free(dev);
        return;
    }
    
    // 通过名称查找设备对象（验证对象管理系统）
    mfrc522_device *found_dev = mfrc522_find(dev_name);
    if (found_dev == RT_NULL) {
        rt_kprintf("Error: Device %s not found in object list!\n", dev_name);
        rt_free(dev);
        return;
    }
    
    rt_kprintf("Device initialized and found successfully\n");
    
    while (1) {
        // 检测卡片
        if (mfrc522_detect_card(found_dev)) {
            rt_kprintf("Card detected!\n");
            retry_count = 0;
            
            // 读取UID
            MFRC522_Status status = mfrc522_read_uid(found_dev, &uid);
            if (status == STATUS_OK) {
                rt_kprintf("UID(%d bytes): ", uid.size);
                for (int i = 0; i < uid.size; i++) {
                    rt_kprintf("%02X ", uid.uid[i]);
                }
                rt_kprintf("\n");
                
                // 卡片类型识别
                if (uid.size == 4) {
                    rt_kprintf("Card type: MIFARE Classic 1K\n");
                } else if (uid.size == 7) {
                    rt_kprintf("Card type: MIFARE Ultralight\n");
                }
            } else {
                rt_kprintf("UID read error: %d\n", status);
            }
        } else {
            if (retry_count++ > 10) {
                rt_kprintf("No card present\n");
                retry_count = 0;
            }
        }
        
        rt_thread_mdelay(300); // 300ms检测间隔
    }
}

/* 创建测试线程 */
int mfrc522_test_init(void)
{
    rt_thread_t tid;

    tid = rt_thread_create("mfrc522_test",
                           mfrc522_test_thread_entry,
                           RT_NULL,
                           1024,
                           RT_THREAD_PRIORITY_MAX / 2,
                           20);
    if (tid != RT_NULL) {
        rt_thread_startup(tid);
        return RT_EOK;
    }
    return -RT_ERROR;
}
//MSH_CMD_EXPORT(mfrc522_test_init, mfrc522 test init);
/* Public functions --------------------------------------------------------- */


#ifdef __cplusplus
}
#endif
// <<< end of configuration section >>>
/* ----------------------------- end of mfrc522_example.c ---------------------------- */
